package org.batch.service.business.impl;

import org.batch.mapper.AccountInfoMapper;
import org.batch.service.business.AccountService;
import org.cleaver.object.number.IntegerUtil;
import org.custom.spring.boot.entity.batch.AccountInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;

/**
 * 账户相关业务接口实现
 *
 * @author LiKun
 * @date 2021/9/29 14:24
 */
@Service
public class AccountServiceImpl implements AccountService {
    @Resource
    private AccountInfoMapper accountInfoMapper;

    @Override
    @Transactional
    public void updateBalance(@NotBlank String id, @NotNull BigDecimal amount) throws Exception {
        AccountInfo account = accountInfoMapper.selectById(id);
        if (IntegerUtil.moreThanZero(account.getBalance().compareTo(amount))) {
            accountInfoMapper.updateBalance(id, amount, account.getVersion());
        } else {
            throw new Exception("账户余额不足");
        }
    }
}
